import axios from 'axios'
import {useUserStore} from '@/store'

axios.defaults.withCredentials=true

// const baseURL = 'http://localhost:8081'
const baseURL = '/api'

const instance = axios.create({
    //基础地址，超时时间
    baseURL,
    //Todo 延长响应时间，方便测试
    timeout: 10000000
})

//请求拦截器
instance.interceptors.request.use(
    (config) => {
        //携带token
        const userStore = useUserStore()
        if (userStore.token) {
            config.headers.token = userStore.token
        }
        return config
    }, (err) => Promise.reject(err)
)

//响应拦截器
instance.interceptors.response.use(
    (res) => {
        //TODO 3.处理业务失败
        //TODO 4.提取核心响应数据
        return res
    }, (err) => {
        //TODO 5.处理401错误
        return Promise.reject(err)
    }
)

export default instance
export {baseURL}